#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct student {
    int age;
    char name[20];
} STU;

void *create(void *arg) {  // 修正返回类型为 void*
    STU *temp = (STU *)arg;
    printf("The following is transferred to thread\n");
    printf("STU age is %d\n", temp->age);
    printf("STU name is %s\n", temp->name);
    return NULL;  // 显式返回（虽示例中无实质用途，但符合规范）
}

int main(int argc, char *argv[]) {
    pthread_t tidp;
    int error;
    STU *stu = malloc(sizeof(STU));
    if (stu == NULL) {  // 检查内存分配是否成功
        perror("malloc");
        return -1;
    }
    stu->age = 20;
    strcpy(stu->name, "abcdefg");
    error = pthread_create(&tidp, NULL, create, (void *)stu);
    if (error != 0) {
        printf("pthread_create failed ");
        free(stu);  // 分配失败时释放内存
        return -1;
    }
    pthread_join(tidp, NULL);
    free(stu);  // 使用完后释放内存
    return 0;
}
